﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NF_Tools
{
    public partial class BF_Menu : UserControl
    {
        public BF_Menu()
        {
            InitializeComponent();
        }

        /// <summary>
        /// LODE
        /// </summary>
        private void PF_Menu_Load(object sender, EventArgs e)
        {
            CreatControl();
        }

        private Label CheckLable = new Label();

        #region - 自定义属性 -
        
        
        private string[] lstitem;
        private Color me_backcolor = Color.White;
        private Color me_honverbackcolor = Color.RoyalBlue;
        private Color me_fontcolor = Color.Black;
        private Color me_hoverfontcolor = Color.White;
        private Color me_mouesdowfontcolor = Color.SlateBlue;
        private Font me_font = new Font(@"微软雅黑", 12);
        private float rowHeight = 40F;

        [Category("自定义属性"), Description("菜单项")]
        public string[] LstItem
        {
            get { return lstitem; }
            set
            {
                lstitem = value;
                CreatControl();
            }

        }

        [Category("自定义属性"), Description("菜单背景色")]
        public Color Me_BackColor
        {
            get { return me_backcolor; }
            set 
            { 
                me_backcolor = value;
                CreatControl(); 
            }
        }

        [Category("自定义属性"), Description("热点背景色")]
        public Color Me_HoverBackColor
        {
            get { return me_honverbackcolor; }
            set 
            { 
                me_honverbackcolor = value;
                CreatControl(); 
            }
        }

        [Category("自定义属性"), Description("默认字颜色")]
        public Color Me_FontColor
        {
            get { return me_fontcolor; }
            set 
            { 
                me_fontcolor = value; 
                CreatControl();
            }
        }

        [Category("自定义属性"), Description("热点字颜色")]
        public Color Me_HoverFontColor
        {
            get { return me_hoverfontcolor; }
            set 
            { 
                me_hoverfontcolor = value; 
                CreatControl(); 
            }
        }

        [Category("自定义属性"), Description("行高")]
        public float RowHeight
        {
            get { return rowHeight; }
            set 
            { 
                rowHeight = value; 
                CreatControl(); 
            }
        }

        [Category("自定义属性"),Description("鼠标划过字体颜色")]
        public Color Me_mouesdowfontcolor
        {
            get { return me_mouesdowfontcolor; }
            set { me_mouesdowfontcolor = value; }
        }

        #endregion

        #region - 自定义事件 -

        public delegate void UserClickEventHand(object sender, EventArgs e,int index);
        [Category("自定义事件"), Description("单击时间")]
        public event UserClickEventHand UserClickEvent;

        #endregion

        #region - 方法 -
        
        /// <summary>
        /// 生成菜单项
        /// </summary>
        private void CreatControl()
        {
            if (LstItem == null) return;
            if (LstItem.Length > 0)
            {
                this.Controls.Clear();
                TableLayoutPanel tblp = new TableLayoutPanel();
                tblp.Dock = DockStyle.Fill;
                tblp.RowCount = lstitem.Length + 1;
                tblp.ColumnCount = 1;

                for (int i = 0; i < lstitem.Length; i++)
                {
                    tblp.RowStyles.Add(new RowStyle(SizeType.Absolute, rowHeight));

                    Label lbl = new Label();
                    lbl.Font = me_font;
                    lbl.ForeColor = Me_FontColor;
                    lbl.Text = lstitem[i];
                    lbl.Tag = i;
                    lbl.Margin = new System.Windows.Forms.Padding(0);
                    lbl.Dock = DockStyle.Fill;
                    lbl.TextAlign = ContentAlignment.MiddleCenter;
                    lbl.MouseEnter += new EventHandler(lbl_MouseEnter);
                    lbl.MouseLeave += new EventHandler(lbl_MouseLeave);
                    lbl.Click += new EventHandler(lbl_Click);

                    if (i == 0)
                    {
                        lbl.BackColor = Me_HoverBackColor;
                        lbl.ForeColor = Me_HoverFontColor;
                        CheckLable = lbl;
                    }

                    tblp.Controls.Add(lbl);
                }
                tblp.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
                this.Controls.Add(tblp);
            }
        }

        #endregion

        #region - 系统事件 -

        /// <summary>
        /// 单击事件
        /// </summary>
        void lbl_Click(object sender, EventArgs e)
        {
            CheckLable.ForeColor = Me_FontColor;
            CheckLable.BackColor = Me_BackColor;

            CheckLable = sender as Label;
            CheckLable.BackColor = Me_HoverBackColor;
            CheckLable.ForeColor = Me_HoverFontColor;

            if (UserClickEvent != null)
                UserClickEvent(sender, e, Convert.ToInt32(CheckLable.Tag));

        }


        /// <summary>
        /// 鼠标离开控件
        /// </summary>
        void lbl_MouseLeave(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            if (CheckLable != lbl)
            {
                lbl.BackColor = Me_BackColor;
                lbl.ForeColor = me_fontcolor;
            }
                
        }

        /// <summary>
        /// 鼠标进入控件
        /// </summary>
        void lbl_MouseEnter(object sender, EventArgs e)
        {
            Label lbl = sender as Label;
            lbl.BackColor = Me_HoverBackColor;
            lbl.ForeColor = Me_mouesdowfontcolor;
        }
        
        #endregion



    }
}
